package com.pi.shisan.web.controller;

import com.pi.shisan.service.dto.AudioInfoDTO;
import com.pi.shisan.service.dto.ClockInfoDTO;
import com.pi.shisan.service.dto.Result;
import com.pi.shisan.service.dto.RunnableClockInfo;
import com.pi.shisan.service.exception.BusinessException;
import com.pi.shisan.service.service.ClockInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/clock")
public class ClockInfoController {

    @Autowired
    private ClockInfoService clockInfoService;

    @GetMapping("/list")
    public Result<List<ClockInfoDTO>> clockPage() {
        return Result.ok(clockInfoService.getClockList());
    }

    @GetMapping("/clock/{id}")
    public Result<ClockInfoDTO> clockById(@PathVariable("id") Long id) {
        return Result.ok(clockInfoService.getClockById(id));
    }

    @GetMapping("/next")
    public Result<RunnableClockInfo> getNextClock() {
        return Result.ok(clockInfoService.getNextClockInfo(false));
    }

    @PostMapping("/createOrUpdate")
    public Result<Long> createOrUpdate(@RequestBody ClockInfoDTO clockInfoDTO) {
        return clockInfoService.createOrUpdateClock(clockInfoDTO);
    }

    @PostMapping("/addOrUpdateClockAudio")
    public Result<Long> createOrUpdate(@RequestBody AudioInfoDTO audioInfoDTO) {
        return Result.ok(clockInfoService.addOrUpdateClockAudio(audioInfoDTO));
    }

    @PostMapping("/volumeSet")
    public Result<Void> updateVolume(@RequestBody ClockInfoDTO clockInfoDTO) {
        clockInfoService.updateVolume(clockInfoDTO.getId(), clockInfoDTO.getClkVolume());
        return Result.ok();
    }

    @PostMapping("/switch")
    public Result<Long> onOffClock(@RequestBody ClockInfoDTO clockInfoDTO) throws Exception{
        clockInfoService.onOffClock(clockInfoDTO);
        return Result.ok();
    }


    @PostMapping("/deleteClock")
    public Result<Void> deleteClock(@RequestBody ClockInfoDTO clockInfoDTO) {
        Long id = clockInfoDTO.getId();
        if (id == null || id <= 0) {
            throw new BusinessException("参数错误");
        }
        clockInfoService.deleteClock(id);
        return Result.ok();
    }


    @PostMapping("/deleteAudio")
    public Result<Void> deleteAudio(@RequestBody AudioInfoDTO audioInfoDTO) {
        Long id = audioInfoDTO.getId();
        if (id == null || id <= 0) {
            throw new BusinessException("参数错误");
        }
        clockInfoService.deleteAudio(id);
        return Result.ok();
    }

    @PostMapping("/clearHour")
    public Result<Void> clearHourInfo(@RequestBody ClockInfoDTO clockInfoDTO) {
        clockInfoService.clearHourInfo(clockInfoDTO.getId());
        return Result.ok();
    }

    @PostMapping("/clearWeek")
    public Result<Void> clearWeekInfo(@RequestBody ClockInfoDTO clockInfoDTO) {
        clockInfoService.clearWeekInfo(clockInfoDTO.getId());
        return Result.ok();
    }

    @PostMapping("/clearDay")
    public Result<Void> clearDayInfo(@RequestBody ClockInfoDTO clockInfoDTO) {
        clockInfoService.clearDayInfo(clockInfoDTO.getId());
        return Result.ok();
    }

    @PostMapping("/clearMonth")
    public Result<Void> clearMonthInfo(@RequestBody ClockInfoDTO clockInfoDTO) {
        clockInfoService.clearMonthInfo(clockInfoDTO.getId());
        return Result.ok();
    }

    @PostMapping("/clearYear")
    public Result<Void> clearYearInfo(@RequestBody ClockInfoDTO clockInfoDTO) {
        clockInfoService.clearYearInfo(clockInfoDTO.getId());
        return Result.ok();
    }
}
